
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Vector;

import cii_lib.CCII_CONFIG_TABLE;

public class InputTaker {

	public InputTaker () {
		this.init(); 
	}
	public InputTaker (String arg) {
		this.pathing = arg;
		this.init();
	}
	
	public int lastCompPos =0;
	public int CompCount =0;
	private int TopMagW;
	
	//public CII_LIB_TEST_cfg_tab as;
	public static String readfromfile;
	
	public String pathing = "";
	public Vector<Famili> fam = new Vector<Famili>();
	public Vector<CII_Tree_Vectors> tabl = new Vector<CII_Tree_Vectors>();

	
	public static void main(String[] args){
		
	InputTaker aha = new InputTaker();
	
	}
	public void init() {
		Vector<CII_Tree_Vectors> vertic = new Vector<CII_Tree_Vectors>();
		CII_Tree_Vectors horis= new CII_Tree_Vectors();
		Vector<CII_Tree_Vectors> Hgood= new Vector<CII_Tree_Vectors>();
		try {
		readfromfile = readFileAsString(pathing);
		StringTokenizer tra = new StringTokenizer(readfromfile);
		
		System.out.println(tra.countTokens()+ " linii: " + getFileLinesCount(pathing));
		for (int i=0;tra.hasMoreTokens();i++) {
			String[] text2 = new String[90];
			horis = new CII_Tree_Vectors();
			//System.out.println(tra.nextToken("()"));
			text2[1]=tra.nextToken("("+")");
			//System.out.println("**********************************************************************");
			//System.out.println(i+"  "+ text2[1]);
			StringTokenizer temp = new StringTokenizer(text2[1]);
			//System.out.println(text2[i]);
			for (int k=0;temp.hasMoreTokens();k++) {
				//System.out.println(k+"  : " + temp.nextToken(","));
				horis.add(temp.nextToken(","+" \t"));
			}
			
			vertic.add(i,horis);
			
		}
		
		
		for (int i = 0 ; i<vertic.size(); i++) {
		
			if (vertic.get(i).size()==11) Hgood.add(vertic.get(i));
		}
		TopMagW = new Integer(Hgood.get(0).get(3));
		tabl = Hgood;
		} catch (Exception fe) {
			System.out.println("Error while processing context file\n"+fe);
		}
	}
	
	public int getMainWidth() {
		return this.TopMagW;
	}
	
	private static String readFileAsString(String filePath) {
        try {
        	StringBuffer fileData = new StringBuffer(1000);
        
        BufferedReader reader = new BufferedReader(
                new FileReader(filePath));
        char[] buf = new char[1024];
        int numRead=0;
       
        while((numRead=reader.read(buf)) != -1){
        	
            String readData = String.valueOf(buf, 0, numRead);
            fileData.append(readData);
            buf = new char[1024];
        }
        reader.close();
       
        
        return fileData.toString();
        } catch (IOException e) {System.out.println(e);}
        return null;
    }
	
	public int getFileLinesCount(String path){
		int lines=0;
		try{
			BufferedReader reader = new BufferedReader(
		
                new FileReader(path));
        while (reader.readLine() != null) {
        	
        	lines++;
        }
        reader.close();
		} catch (IOException e) {System.out.println("Err in lines count \n" +e);}
		
        return lines;
	}

	public Object StepMatrixExt (Vector main,int h, int w) {
		
		Vector tmp2 = (Vector)main.get(h);
		
		return tmp2.get(w);
	}

	public Object StepMatrix (int h,int w) {
		
		return tabl.get(h).get(w);
	}
	//TODO object
	public CII_Tree_Vectors getNextComp() {
	int inc=lastCompPos;
		while(inc < tabl.size()) {
			if (tabl.get(inc).get(2).equals("CIIlib.TABLE_TYPE.COMP")) {
			inc++;
			break;
			}
			inc++;
		}
		lastCompPos=inc;
		int parent = new Integer(tabl.get(lastCompPos-1).get(9));
		//TODO
		
		if (tabl.get(lastCompPos-1).get(9).equals("0")) {}
		CompCount++;
		return tabl.get(lastCompPos-1);
	}
	
	public Vector<CII_Tree_Vectors> getCompTokens(int compID) {
	int inc=0,inc2=0;	
	Vector<CII_Tree_Vectors> wek = new Vector<CII_Tree_Vectors>();
		while(inc < tabl.size()) {
			int tmp = new Integer(tabl.get(inc).get(0));
			//System.out.println(compID +" "+ tmp);
			if (tmp == compID) {
				if (tabl.get(inc).get(2).equals("CIIlib.TABLE_TYPE.AREA") ||
					tabl.get(inc).get(2).equals("CIIlib.TABLE_TYPE.WORD") ||
					tabl.get(inc).get(2).equals("CIIlib.TABLE_TYPE.BITS")) {
					
					wek.add(inc2,tabl.get(inc));
					inc2++;
				}
				if (tabl.get(inc).get(7).equals("0") && tabl.get(inc).get(8).equals("0") && tabl.get(inc).isParam()) {
					wek.add(inc2,tabl.get(inc));
					
					inc2++;
				}
				
			}
			
			inc ++;
		}
		return wek;
	}
	
	public Vector<CII_Tree_Vectors> getCompExtraTokens(int compID) {
		int inc=0,inc2=0;	
		Vector<CII_Tree_Vectors> wek = new Vector<CII_Tree_Vectors>();
			while(inc < tabl.size()) {
				int tmp = new Integer(tabl.get(inc).get(0));
				if (tmp == compID) {
						wek.add(inc2,tabl.get(inc));
						inc2++;
				}
				
				inc ++;
			}
			return wek;
		}
	
	public CII_Tree_Vectors getNextLine(CII_Tree_Vectors from, int ofComp) {
		
		Vector<CII_Tree_Vectors> tmp = new Vector<CII_Tree_Vectors>();
		tmp = getCompExtraTokens(ofComp);
		for (int i=0;i<tmp.size();i++) {
			if (tmp.get(i).equals(from)) {
				return tmp.get(i+1);
			}
		}
		
		return null;
	}
	
	public int countAddr(CII_Tree_Vectors atryn, int x,int y) {
		int wi = new Integer(atryn.get(3));
		int adr_st = new Integer(atryn.get(7));
//		int gr = new Integer(atryn.get(5));
		int ilWgr = new Integer(atryn.get(4));
		return (y*wi+adr_st*8+x*ilWgr*8);
	}
	
	public CII_Tree_Vectors getRoot() {
		if (tabl.get(0).get(1).charAt(1)=='$') return tabl.get(0);
		return null;
	}
	
	public long LongFromU2(String arg) {
		long ltmp =0;
		for(int q=0;q<arg.length();q++) {
			ltmp = ltmp+(long)Math.pow(2, q) * (new Long(arg.charAt(arg.length()-q-1))-48);
		}
		return ltmp;
	}
	
	public void Reset() {
		lastCompPos=0;
	}
}
